"""
Case Type   : datakit并行安装数据库
Case Name   : 创建并行安装任务，点击安装用户下拉框，显示已添加的用户
Create At   : 2025/03/28
Owner       : @zhao-pengcheng-g
Description :
    1.导入服务器
    2.进入并行安装任务界面
    3.查看已添加的用户
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.datakit_api.BaseOpsApi import BaseOpsApi
from testcase.utils.CommonDatakit import CommonDatakit
from testcase.utils.Common import Common


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.base_ops_api = BaseOpsApi(self.token)
        self.datakit_sh = CommonDatakit(self.token)
        self.pri_node = Node('PrimaryDbUser')
        self.pri_node_root = Node('PrimaryRoot')
        self.host_list = [self.pri_node_root]
        self.common = Common('PrimaryRoot')

    def test_batch_installation(self):
        text = '----step1:导入服务器 expect:成功----'
        self.log.info(text)
        self.datakit_sh.import_host(self.host_list)
        self.common.create_user(self.pri_node_root, "Batch_0002")

        text = '----step2:添加用户 expect:成功----'
        self.log.info(text)
        res3 = self.com_api.get_host_page(name=self.pri_node_root.ssh_host)
        self.data3 = {"hostId": f"{res3.json()['rows'][0]['hostId']}",
                      "username": "Batch_0002",
                      "password": f"{macro.COMMON_PASSWD}"}
        res4 = self.com_api.post_hostUser(self.data3)
        self.log.info(res4.json())
        self.assertEqual(200, res4.json()['code'], '添加用户失败')

        text = '----step3:获取用户列表 expect:成功----'
        self.log.info(text)
        res5 = self.base_ops_api.get_plugins_base_ops_cluster_Task_host_user(f"{res3.json()['rows'][0]['hostId']}")
        self.log.info(res5.text)
        self.assertTrue("Batch_0002" in res5.text, "添加用户失败")

    def tearDown(self):
        text = '----step4:清理环境 expect：成功----'
        self.log.info(text)
        self.datakit_sh.delete_host()
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
